/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sabatec.controllers;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import br.com.sabatec.entities.ObraExecSabatec;
import br.com.sabatec.entities.RequestServiceSabatec;
import br.com.sabatec.service.ObraExecSabatecService;
import br.com.sabatec.service.RequestSabatecService;

/**
 * 
 * @author Daniel.Morais
 */
@ManagedBean(name = "obraExcecSabatecBean")
@SessionScoped
public class ObraExecSabatecBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private ObraExecSabatec obraExecSabatec = new ObraExecSabatec();
	private RequestServiceSabatec requestService = new RequestServiceSabatec();
	private List<ObraExecSabatec> obras;

	public ObraExecSabatec getObraExecSabatec() {
		return obraExecSabatec;
	}

	public void setObraExecSabatec(ObraExecSabatec obraExecSabatec) {
		this.obraExecSabatec = obraExecSabatec;
	}

	public RequestServiceSabatec getRequestService() {
		return requestService;
	}

	public void setRequestService(RequestServiceSabatec requestService) {
		this.requestService = requestService;
	}

	public List<ObraExecSabatec> getObras() {
		return obras;
	}

	public void setObras(List<ObraExecSabatec> obras) {
		this.obras = obras;
	}


	public String getExecObra(RequestServiceSabatec request) {

		this.requestService = request;
		this.obraExecSabatec.setIdservice(request);
		try {
			this.obras = ObraExecSabatecService.listarTodos(this.obraExecSabatec);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "cadastraMaoObra";

	}

	public void salva(ObraExecSabatec obra) {

		this.obraExecSabatec.setIdservice(this.requestService);
		FacesMessage msg = null;
		boolean loggedIn = false;
		RequestContext context = RequestContext.getCurrentInstance();
		try {
			loggedIn = true;
			ObraExecSabatecService.salva(obra);
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Cadastro realizado com sucesso", "");
			this.obras = ObraExecSabatecService.listarTodos(this.obraExecSabatec);
		} catch (Exception e) {
			e.printStackTrace();
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"Problema ao realizar o cadastro", "");
		}
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);
	}
	
	public void salvaUpdate(ObraExecSabatec obra) {

		RequestContext context = RequestContext.getCurrentInstance();
		FacesMessage msg;
		boolean loggedIn;

		try {
			loggedIn = true;
			ObraExecSabatecService.salvaUpdate(obra);
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Cadastro realizado com sucesso", "");
			this.obras = ObraExecSabatecService.listarTodos(this.obraExecSabatec);
			showDialogClose();
		} catch (Exception e) {
			e.printStackTrace();
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"Problema ao realizar o cadastro", "");
			showDialogClose();
		}
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);
	}
	
	public void exclui(ObraExecSabatec obra) {

		this.obraExecSabatec.setIdservice(this.requestService);
		FacesMessage msg = null;
		boolean loggedIn = false;
		RequestContext context = RequestContext.getCurrentInstance();
		try {
			loggedIn = true;
			ObraExecSabatecService.exclui(obra);
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Registro excluido com sucesso", "");
			this.obras = ObraExecSabatecService.listarTodos(this.obraExecSabatec);
		} catch (Exception e) {
			e.printStackTrace();
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"Problema ao excluir registro", "");
		}
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);

	}
	
	public void showDialog() {
		RequestContext.getCurrentInstance().execute("dlg2.show()");
	}
	
	public void showDialogClose() {
		RequestContext.getCurrentInstance().execute("dlg2.hide()");
	}

}
